package com.jzo2o.customer.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.mapper.ICertificationByAgencyMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationRecording;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.ICertificationByAgencyService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ICertificationByAgencyServiceImpl extends ServiceImpl<ICertificationByAgencyMapper, AgencyCertificationRecording> implements ICertificationByAgencyService {

    @Resource
    private ICertificationByAgencyMapper certificationByAgencyMapper;

    @Resource
    private AgencyCertificationMapper agencyCertificationMapper;


    /**
     * 机构提交认证
     * @param addReqDTO
     */
    @Override
    public void addCertificationAudit(AgencyCertificationAuditAddReqDTO addReqDTO) {
        if(ObjectUtil.isEmpty(addReqDTO)){
            throw new ForbiddenOperationException("注册信息不能为空哦");
        }
        Long id = UserContext.currentUserId();
        AgencyCertificationRecording agencyCertificationRecording
                = BeanUtil.copyProperties(addReqDTO, AgencyCertificationRecording.class);
        agencyCertificationRecording.setCertificationStatus(1);
        agencyCertificationRecording.setAuditStatus(0);
        agencyCertificationRecording.setServeProviderId(id);
        certificationByAgencyMapper.insert(agencyCertificationRecording);

        AgencyCertification agencyCertification = agencyCertificationMapper.selectById(id);
        AgencyCertification insertAgency = BeanUtil.copyProperties(agencyCertificationRecording, AgencyCertification.class);
        if(ObjectUtil.isEmpty(agencyCertification)){
            insertAgency.setCertificationStatus(1);
            insertAgency.setId(id);
            agencyCertificationMapper.insert(insertAgency);
        }else{
            insertAgency.setCertificationStatus(1);
            insertAgency.setId(id);
            agencyCertificationMapper.updateById(insertAgency);
        }

    }

    /**
     * 获取驳回原因
     */
    @Override
    public RejectReasonResDTO getRejectReason() {
        Long id = UserContext.currentUserId();
        AgencyCertification temp  = certificationByAgencyMapper.selectLastRejectReason(id);
        RejectReasonResDTO rejectReasonResDTO = new RejectReasonResDTO();
        rejectReasonResDTO.setRejectReason(temp.getRejectReason());
        return rejectReasonResDTO;
    }
}
